### Cleaning Code for "Value Similarity and Norm Change: Null Effects and Backlash to Messaging on Same-Sex Rights in Uganda"
### Purpose: to prepare raw data for all subsequent analysis
### Author: Nicholas Lyon


# Preliminaries -----------------------------------------------------------

# Purpose of code section: load required libraries and raw data

library('rio')
library('tidyverse')

root <- "add your path here/" # add path to where "uganda_samesex_replication" is stored
setwd(paste0(root, "uganda_samesex_replication/Data"))

raw_data <- import("uganda_raw.csv") # bring in raw survey data


# Define treatment variable -----------------------------------------------

# Purpose of code section: defining African, Western, and Placebo treatment conditions

# Randomization to treatment arms conducted within SurveyCTO based on random number generator:
# African message = 2/3 < random number < 1
# Western message = 1/3 < random number < 2/3
# Placebo message = 0 < random number < 1/3

clean_data <- raw_data %>%
  mutate(treatment = case_when(
    randomize_recording > 2/3 ~ 'african',
    randomize_recording < 2/3 & randomize_recording > 1/3 ~ 'western',
    randomize_recording < 1/3 ~ 'placebo'
  ))


# Process analysis variables -----------------------------------------------

# Purpose of code section: prepare all outcome variables used for analysis in main text

# Perceptions towards similarity of African and Western Values
# values_west and values_africa raw code: 1 = most similar; 5 = least similar; 99 = not sure
# values_compare raw code: 1 = African more similar; 2 = Western more similar; 3 = same; 99 = not sure; 999 = refused to answer

clean_data <- clean_data %>%
  mutate(values_west = case_when(
    values_west == 1 ~ 4,
    values_west == 2 ~ 3,
    values_west == 3 ~ 2,
    values_west == 4 ~ 1,
    values_west == 5 ~ 0,
    values_west == 99 ~ 99
  ),
  values_africa = case_when(
    values_africa == 1 ~ 4,
    values_africa == 2 ~ 3,
    values_africa == 3 ~ 2,
    values_africa == 4 ~ 1,
    values_africa == 5 ~ 0,
    values_africa == 99 ~ 99
  ),
  values_compare = case_when(
    values_compare == 1 ~ 1,
    values_compare == 2 ~ -1,
    values_compare == 3 ~ 0,
    values_compare == 99 ~ 99,
    values_compare == 999 ~ NA_real_
  ))

# Manipulation checks
# legal_nonafrican and legal_african raw code: 1 = yes; 2 = no; 999 = refused to answer. Coded such that it is an indicator of identifying legality or not (refused to answer set to zero)
# nonafrican_welcome_homosexuals and african_welcome_homosexuals: 1 = more welcoming; 2 = as welcoming; 3 = less welcoming; 99 = not sure; 999 = refused to answer
clean_data <- clean_data %>%
  mutate(legal_nonafrican = case_when(
    legal_nonafrican == 1 ~ 1,
    legal_nonafrican == 2 ~ 0,
    legal_nonafrican == 999 ~ 0 
  ),
  legal_africa = case_when(
    legal_africa == 1 ~ 1,
    legal_africa == 2 ~ 0,
    legal_africa == 999 ~ 0
  ),
  nonafrican_welcome_homosexuals = case_when(
    nonafrican_welcome_homosexuals == 1 ~ 1,
    nonafrican_welcome_homosexuals == 2 ~ 0,
    nonafrican_welcome_homosexuals == 3 ~ -1
  ),
  african_welcome_homosexuals = case_when(
    african_welcome_homosexuals == 1 ~ 1,
    african_welcome_homosexuals == 2 ~ 0,
    african_welcome_homosexuals == 3 ~ -1
  ))

# Social distance measure 
# each level is asked as a separate question: code continuous measure that indicates the highest level at which respondent would interact
# raw code: _1 = family; _2 = friend; _3 = neighbor; _4 = coworker; _5 = citizen; _6 = foreigner; _7 = none of above

clean_data <- clean_data %>%
  mutate(social_distance = case_when(
    relations_homosexuals_1 == 1 ~ 6,
    relations_homosexuals_2 == 1 & relations_homosexuals_1 != 1 ~ 5,
    relations_homosexuals_3 == 1 & relations_homosexuals_1 != 1 
    & relations_homosexuals_2 !=1 ~ 4,
    relations_homosexuals_4 == 1 & relations_homosexuals_1 != 1 
    & relations_homosexuals_2 !=1  & relations_homosexuals_3 != 1 ~ 3,
    relations_homosexuals_5 == 1 & relations_homosexuals_1 != 1 
    & relations_homosexuals_2 !=1  & relations_homosexuals_3 != 1
    & relations_homosexuals_4 != 1 ~ 2,
    relations_homosexuals_6 == 1 & relations_homosexuals_1 != 1 
    & relations_homosexuals_2 !=1  & relations_homosexuals_3 != 1
    & relations_homosexuals_4 != 1 & relations_homosexuals_5 != 1 ~ 1,
    relations_homosexuals_7 == 1 & relations_homosexuals_1 != 1 
    & relations_homosexuals_2 !=1  & relations_homosexuals_3 != 1
    & relations_homosexuals_4 != 1 & relations_homosexuals_5 != 1
    & relations_homosexuals_6 != 1 ~ 0
  ))

# Same sex rights index
# services_homosexuals raw code: 1 = should have same access as others; 2 = should not have same access as others
# rights_homosexuals raw code: 1 = should be protected against discrimination; 2 = should not be protected against discrimination
# rent_homosexual: 1 = refusing to rent always justifiable; 2 = refusing to rent usually justifiable; 3 = refusing to rent sometimes justifiable; 4 = refusing to rent never justifiable
# beating_homosexual: 1 = beating always justifiable; 2 = beating usually justifiable; 3 = beating sometimes justifiable; 4 = beating never justifiable
# imprison_homosexual: 1 = strongly agree; 2 = agree; 3 = neither agree nor disagree; 4 = disagree; 5 = strongly disagree 

clean_data <- clean_data %>%
  mutate(services_h = case_when(
    services_homosexuals == 2 ~ 0,
    services_homosexuals == 1 ~ 1
  ),
  rights_h = case_when(
    rights_homosexuals == 2 ~ 0,
    rights_homosexuals == 1 ~ 1
  ),
  rent_h = case_when(
    rent_homosexual == 1 ~ 0,
    rent_homosexual == 2 ~ 1/3,
    rent_homosexual == 3 ~ 2/3,
    rent_homosexual == 4 ~ 1
  ),
  beat_h = case_when(
    beating_homosexual == 1 ~ 0,
    beating_homosexual == 2 ~ 1/3,
    beating_homosexual == 3 ~ 2/3,
    beating_homosexual == 4 ~ 1
  ),
  imprison_h = case_when(
    imprison_homosexual == 1 ~ 0,
    imprison_homosexual == 2 ~ 1/4,
    imprison_homosexual == 3 ~ 1/2,
    imprison_homosexual == 4 ~ 3/4,
    imprison_homosexual == 5 ~ 1
  ))

clean_data$ss_rights_index = rowMeans(clean_data[,c("services_h", "rights_h", "rent_h",
                                          "beat_h", "imprison_h")], na.rm = T) # take mean across measures to form index

# Morality Index
# men_consensual raw code: 1 = not wrong at all; 2 = wrong only sometimes; 3 = almost always wrong; 4 = always wrong
# women_consensual raw code: 1 = not wrong at all; 2 = wrong only sometimes; 3 = almost always wrong; 4 = always wrong

clean_data <- clean_data %>%
  mutate(men_consensual = case_when(
    men_consensual == 1 ~ 1,
    men_consensual == 2 ~ 2/3,
    men_consensual == 3 ~ 1/3,
    men_consensual == 4 ~ 0
  ),
  women_consenting = case_when(
    women_consenting == 1 ~ 1,
    women_consenting == 2 ~ 2/3,
    women_consenting == 3 ~ 1/3,
    women_consenting == 4 ~ 0
  ))

clean_data$ss_morality_index = rowMeans(clean_data[,c("men_consensual", "women_consenting")],
                                   na.rm = T) # take mean across measures to form index

# Two behavioral measures
# homosexual_message raw code: 1 = Uganda should do more; 2 = Uganda should not do more; 3 = do not send message
# destruction game raw code: 1 = 1000 USh to both; 2 = eliminate LGBT+ donation; 3 = eliminate education donation

clean_data <-  clean_data %>%
  mutate(homosexual_message = case_when(
    homosexual_message == 1 ~ 1,
    homosexual_message == 2 ~ -1,
    homosexual_message == 3 ~ 0),
    destruction_game = case_when(
      destruction_game == 1 ~ 0,
      destruction_game == 3 ~ 0,
      destruction_game == 2 ~ 1
    ))

# List experiment: create separate data frame for list experiment analysis
# There are two different lists, and sensitive item is randomly included in one for each respondent
# Order of the two lists is also randomized
# let list A = list that starts w/ Christians; list B = list that starts w/ alcoholics
# position1_list1: respondent first asked about list A with the sensitive item 
# position1_list2: respondent asked second about list B w/out sensitive item
# position2_list1: respondent first asked about list B w/out sensitive item
# position2_list2: respondent asked second about list A with the sensitive item
# position3_list1: respondent first asked about list B with the sensitive item
# position3_list2: respondent asked second about list A w/out sensitive item
# position4_list1: respondent first asked about list A w/out sensitive item
# position4_list2: respondent asked second about list B with the sensitive item
# variable values indicate number of different types respondent would be willing to have live in their village 

list_analysis <- clean_data %>%
  select(position1_list1, position1_list2,
         position2_list1, position2_list2,
         position3_list1, position3_list2,
         position4_list1, position4_list2,
         treatment) %>% 
  # defining list1 as all responses to list A
  mutate(list1 = case_when(
    !is.na(position1_list1) ~ position1_list1,
    !is.na(position2_list2) ~ position2_list2,
    !is.na(position3_list2) ~ position3_list2,
    !is.na(position4_list1) ~ position4_list1
  ),
  # defining list2 as all responses to list B
  list2 = case_when(
    !is.na(position1_list2) ~ position1_list2,
    !is.na(position2_list1) ~ position2_list1,
    !is.na(position3_list1) ~ position3_list1,
    !is.na(position4_list2) ~ position4_list2
  ),
  # defining indicator for whether sensitive item included in list A
  list1_t = case_when(
    !is.na(position1_list1) ~ 1,
    !is.na(position2_list2) ~ 1,
    !is.na(position3_list2) ~ 0,
    !is.na(position4_list1) ~ 0
  ),
  # defining indicator for whether sensitive item included in list B
  list2_t = case_when(
    !is.na(position1_list2) ~ 0,
    !is.na(position2_list1) ~ 0,
    !is.na(position3_list1) ~ 1,
    !is.na(position4_list2) ~ 1
  ))

# western perceptions index
# record_followexample raw code: 1 = strongly agree; 2 = agree; 3 = neither agree nor disagree; 4 = disagree; 5 = strongly disagree
# care_nonafrica raw code: 1 = they care; 2 = they sometimes care; 3 = they do not care
# follow_western raw code: 1 = follow advice; 2 = follow advice sometimes; 3 = should not follow advice 
clean_data <- clean_data %>%
  mutate(example_nonafrican = case_when(
    record_followexample == 1 ~ 1,
    record_followexample == 2 ~ 3/4,
    record_followexample == 3 ~ 1/2,
    record_followexample == 4 ~ 1/4,
    record_followexample == 5 ~ 0),
    care_nonafrican = case_when(
      care_western == 1 ~ 1,
      care_western == 2 ~ 1/2,
      care_western == 3 ~ 0),
    leaders_follow_nonafrican = case_when(
      follow_western == 1 ~ 1,
      follow_western == 2 ~ 1/2,
      follow_western == 3 ~ 0))

clean_data$nonafrican_perceptions = rowMeans(clean_data[,c("example_nonafrican", 
                                                 "care_nonafrican",
                                                 "leaders_follow_nonafrican")],
                                        na.rm = T) # take mean across measures to form index

# african perceptions index
# record_followafrica raw code: 1 = strongly agree; 2 = agree; 3 = neither agree nor disagree; 4 = disagree; 5 = strongly disagree
# care_african raw code: 1 = they care; 2 = they sometimes care; 3 = they do not care
# follow_african raw code: 1 = follow advice; 2 = follow advice sometimes; 3 = should not follow advice 
clean_data <- clean_data %>%
  mutate(example_african = case_when(
    record_followafrica == 1 ~ 1,
    record_followafrica == 2 ~ 3/4,
    record_followafrica == 3 ~ 1/2,
    record_followafrica == 4 ~ 1/4,
    record_followafrica == 5 ~ 0),
    care_african = case_when(
      care_african == 1 ~ 1,
      care_african == 2 ~ 1/2,
      care_african == 3 ~ 0),
    leaders_follow_african = case_when(
      follow_african == 1 ~ 1,
      follow_african == 2 ~ 1/2,
      follow_african == 3 ~ 0))

clean_data$african_perceptions = rowMeans(clean_data[,c("example_african", 
                                              "care_african",
                                              "leaders_follow_african")],
                                     na.rm = T) # take mean across measures to form index

# Export data files -------------------------------------------------------

# Purpose of code section: export cleaned data for analysis

write.csv(clean_data, file = "clean_data.csv", row.names = F) # cleaned survey data file
write.csv(list_analysis, file = "list_data.csv", row.names = F) # data for list analysis
